import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {ValidateProvider} from "../../providers/validate/validate";
import {ConfigProvider} from "../../providers/config/config";
import {StorageProvider} from "../../providers/storage/storage";
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import {UtilsProvider} from "../../providers/utils/utils";

@IonicPage({
    segment: 'member/story_comment',
    defaultHistory: ['MemberPage']
})
@Component({
    selector: 'page-my-story-comment',
    templateUrl: 'my-story-comment.html',
})
export class MyStoryCommentPage {

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public validate: ValidateProvider,
        public config: ConfigProvider,
        public storage: StorageProvider,
        public httpService: HttpServicesProvider,
        public util: UtilsProvider
    ) {
        if (!this.validate.checkLogin()) {
            this.navCtrl.push('LoginPage');
        }
    }

    //判断用户是否登录
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }

    userInfo = this.config.userInfoHolder;
    ionViewWillEnter() {
        if (!this.validate.checkLogin()) {//登录
            this.navCtrl.push('LoginPage', {'backPage': 'MemberPage'});
        } else {
            this.getData(null);
        }
    }

    page = 1;
    lists = [];
    getData(infiniteScroll) {
        this.httpService.requestPostData('/story/comment', {
            token: this.config.getToken(),
            page: this.page
        }, response => {
            //全部数据
            let data = response['data'];
            this.lists = this.lists.concat(data['lists']);
            this.page++;
            if(infiniteScroll){
                infiniteScroll.complete();   /*请求完成数据更新页面*/
                if(data['lists'].length<data['page_size']){   /*没有数据 禁用上拉更新*/
                    infiniteScroll.enable(false);
                }
            }
        });
    }

    doInfinite(infiniteScroll) {
        this.getData(infiniteScroll);
    }
}
